package com.ticket.sass.common.wxpay;

/**
 * @author ywb
 * @date 2024-05-22 16:45
 */

import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.RequiredArgsConstructor;

import java.math.BigDecimal;

@RequiredArgsConstructor
public class WxPayServiceWrapper {
    
    private final WxPayService wxPayService;
    
    public WxPayServiceWrapper(String appId, String mchId, String mchKey, String p12Content) throws Exception {
        this.wxPayService = WxPayConfigManager.buildWxPayService(appId, mchId, mchKey, p12Content);
    }
    
    public WxPayRefundResult refund(String orderNo, BigDecimal refundFee, BigDecimal totalFee, String desc, String notifyUrl) throws WxPayException {
        WxPayRefundRequest request = new WxPayRefundRequest();
        request.setOutTradeNo(orderNo);
        request.setOutRefundNo(orderNo);
        request.setRefundDesc(desc);
        request.setRefundFee(BaseWxPayRequest.yuanToFen(refundFee.toString()));
        request.setTotalFee(BaseWxPayRequest.yuanToFen(totalFee.toString()));
        if (notifyUrl != null && !"".equals(notifyUrl)){
            request.setNotifyUrl(notifyUrl); // 设置回调地址
        }
        
        return wxPayService.refund(request);
    }
    
    public WxPayRefundQueryResult queryRefund(String outRefundNo) throws WxPayException {
        return wxPayService.refundQuery(null, outRefundNo, null, null);
    }
}

